<?php

require_once 'PHPUnit/Framework/TestCase.php';
require_once 'Inspekt.php';

class Inspekt_Cages extends PHPUnit_Framework_TestCase {

    protected $fixture;

    protected function setUp() {
        $_SERVER = array(
            '_SERVER' => 'caged',
        );
        $_GET = array(
            '_GET' => 'caged',
        );
        $_POST = array(
            '_POST' => 'caged',
        );
        $_COOKIE = array(
            '_COOKIE' => 'caged',
        );
        $_ENV = array(
            '_ENV' => 'caged',
        );
        $_FILES = array(
            '_FILES' => 'caged',
        );
        $_SESSION = array(
            '_SESSION' => 'caged',
        );
        $_REQUEST = array(
            '_REQUEST' => 'caged',
        );
    }

    public function testMakeServerCage() {
        $this->cage_test('Server');
    }

    public function testMakeGetCage() {
        $this->cage_test('Get');
    }

    public function testMakePostCage() {
        $this->cage_test('Post');
    }

    public function testMakeCookieCage() {
        $this->cage_test('Cookie');
    }

    public function testMakeEnvCage() {
        $this->cage_test('Env');
    }

    public function testMakeFilesCage() {
        $this->cage_test('Files');
    }

    public function testMakeSessionCage() {
        $this->markTestSkipped('session cage currently unavailable in Inspekt');
    }

    public function testMakeSuperCage() {
        $cage = Inspekt::makeSuperCage();
        $this->assertNull($_REQUEST);
        $cage_copy = Inspekt::makeSuperCage();
        $this->assertSame( $cage, $cage_copy );
        $this->assertEquals( 'caged', $cage->server->getRaw('_SERVER') );
        $this->assertEquals( 'caged', $cage->get->getRaw('_GET') );
        $this->assertEquals( 'caged', $cage->post->getRaw('_POST') );
        $this->assertEquals( 'caged', $cage->cookie->getRaw('_COOKIE') );
        $this->assertEquals( 'caged', $cage->env->getRaw('_ENV') );
        $this->assertEquals( 'caged', $cage->files->getRaw('_FILES') );
    }

    protected function cage_test($camelcase_cagename) {
        $cage_factory_methodname = 'make' . $camelcase_cagename . 'Cage';
        $allcaps             = strtoupper($camelcase_cagename);
        $superglobal_varname = '_' . $allcaps;
        $globals_key         = 'HTTP_' . $allcaps . '_VARS';

        $cage = Inspekt::$cage_factory_methodname();
        $this->assertNull( $$superglobal_varname );
        $this->assertNull( $GLOBALS[$globals_key] );
        $$superglobal_varname = array( 'ick' => 'gakkk' );
        $cage_copy = Inspekt::$cage_factory_methodname();
        $this->assertSame( $cage, $cage_copy );
        $this->assertFalse( $cage_copy->getRaw('ick') );
        $this->assertEquals( 'caged', $cage->getRaw($superglobal_varname) );
    }

}
